package com.cordershy.service;

import com.cordershy.mapper.userMapper;
import com.cordershy.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class userService {
    @Autowired
    userMapper userMapper;
    @Transactional
    public List<user> selectAll(){
        return userMapper.selectAll();
    }
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.READ_COMMITTED)
    @Cacheable(value = "accountCache",key = "'userid'+#userid")
    public user selectById(int userid){

        return userMapper.selectById(userid);
    }
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    @CachePut(value = "accountCache",key = "'userid'+#user.userid")
    public user insert(user user){
        userMapper.insert(user);
        return user;
    }
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
    @CacheEvict(value = "accountCache",key = "'userid'+#userid")
    public int delete(int userid){
        return userMapper.delete(userid);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    @CachePut(value = "accountCache",key = "'userid'+#user.userid")
    public user update(user user){
        user temp = selectById(user.getUserid());
        if (temp==null){
            return null;
        }else {
            userMapper.update(user);
        }
        return user;
    }
}
